<?php
namespace api\controllers;
use common\models\Shop;
use common\models\Active;
use common\models\UserCollection;

use common\service\ConstantService;
use common\service\GlobalUrlService;
class StoreController extends BaseController
{
    /**
     * 店铺
     */
    public function actionIndex(){
        $id = $this->get('shop_id');

        $shop = Shop::find()
            ->select('id,shop_name,shop_logo,status')
            ->where(['id'=>$id])
            ->asArray()
            ->one();

        if(!$shop || !$id){
            return $this->renderJson([],'该商家不存在',ConstantService::$response_fail);
        }

        switch ($shop['status']){
            case 0:
                return $this->renderJson([],'该商家待审核',ConstantService::$response_fail);
            case 2:
                return $this->renderJson([],'该商家审核未通过',ConstantService::$response_fail);
            case 3:
                return $this->renderJson([],'该商家已停用',ConstantService::$response_fail);
            default;
        }
        $shop['shop_logo'] = GlobalUrlService::buildCdnUrl($shop['shop_logo']);
        $page = intval($this->get('page'));
        $page = ( $page > 0 )? $page : 1;
//        $search_word = $this->get('search_word');
        //排序默认1 2  销量 3 价格高到低  4 价格低到高
        $type = $this->get('type');
        switch ($type){
            case 1:
                $sort = [
                    'set_top' => SORT_DESC ,
                    'sales_volume' => SORT_DESC,
                    'update_time' => SORT_DESC
                ];
                break;
            case 2:
                $sort = [
                    'sales_volume' => SORT_DESC,
                ];
                break;
            case 3://价格从高到低
                $sort = [
                    'max_price' => SORT_DESC,
                ];
                break;
            case 4://价格从低到高
                $sort = [
                    'max_price' => SORT_ASC,
                ];
                break;
            default:
                $sort = [
                    'set_top' => SORT_DESC ,
                    'sales_volume' => SORT_DESC,
                    'update_time' => SORT_DESC
                ];
                break;
        }
        $offset = ($page - 1) * $this->page_size;
        $active_query = Active::find();
        $active_query->andWhere(['status' => 1]);
        $active_query->andWhere(['is_del' => 0]);
        $active_query->andWhere(['shop_id' => $id]);
//        $active_query->andWhere(['like' ,'active_name',$search_word]);
        $shop['data'] = $active_query
            ->select('active_name,id,goods_img,max_price,min_price')
            ->orderBy($sort)
            ->offset( $offset )
            ->limit( $this->page_size )
            ->asArray()
            ->all();
        foreach ($shop['data'] as &$val){
            $val['goods_img'] = GlobalUrlService::buildCdnUrl($val['goods_img']);
        }

        $coll = UserCollection::find()
            ->select('count(id) as count')
            ->where(['list_id'=>$id,'type'=>1])
            ->asArray()
            ->one();
        $shop['count'] = $coll['count'];
        $shop['is_ct'] = 0;
        $iscl = UserCollection::find()
            ->where(['user_id'=>$this->user['id'],'type'=>1,'list_id'=>$id])
            ->asArray()
            ->one();
        if($iscl){
            $shop['is_ct'] = 1;
        }
        return $this->renderJson($shop,'',ConstantService::$response_success);
    }
 }